﻿using System.Collections.Generic;
using System.Data;

namespace ncp.Business
{
    public class GoodsClassBll
    {
        /// <summary>
        /// 得到商品分类信息表
        /// </summary>
        public static DataTable GetGoodsClassTable(string p_Where, string p_Orderby, string pDataBase)
        {
            string sqlStr = "select * from bi_GoodsClass_v";
            if (!string.IsNullOrEmpty(p_Where))
                sqlStr += " where " + p_Where;
            if (!string.IsNullOrEmpty(p_Orderby))
                sqlStr += " order by " + p_Orderby;
            return CommonBll.GetdtList(sqlStr, pDataBase);
        }

        /// <summary>
        /// 得到记录数
        /// </summary>
        public static int GetGoodsClassCount(string pcWhere, string pDataBase)
        {
            string sqlStr = "select count(*) from bi_GoodsClass";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            return CommonBll.ReadInteger(sqlStr, pDataBase);
        }


        /// <summary>
        /// 商品是否存在
        /// </summary>
        public static int IsExistGoods(string GC_Id, string pDataBase)
        {
            string sqlStr = "select count(1) from bi_GoodsInfo where GC_Id='" + GC_Id + "'";
            return CommonBll.ReadInteger(sqlStr, pDataBase);
        }

        /// <summary>
        /// 修改商品分类信息
        /// </summary>
        public static int UpdateGoodsClass(Dictionary<string, object> dicGoodsClass, string pDataBase)
        {
            string sqlStr = "UPDATE bi_GoodsClass " +
                            "SET GC_Name = '" + dicGoodsClass["gc_name"].ToString() + "',GC_Flag='" + dicGoodsClass["gc_flag"].ToString() + "' " +
                            "where GC_Id='" + dicGoodsClass["gc_id"].ToString() + "'";
            return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
        }

        /// <summary>
        /// 增加商品分类信息
        /// </summary>
        public static int InsertGoodsClass(Dictionary<string, object> dicGoodsClass, string pDataBase)
        {
            int GC_Order = CommonBll.ReadInteger("select max(GC_Order) from bi_GoodsClass where GC_PId='" + dicGoodsClass["gc_id"].ToString() + "'", pDataBase)+1;
            string sqlStr = "INSERT INTO dbo.bi_GoodsClass (GC_PId, GC_Name,GC_Status, GC_Order,GC_Level,GC_Flag) " +
                            "VALUES ('" + dicGoodsClass["gc_id"].ToString() + "', '" + dicGoodsClass["gc_name"].ToString() + "', '1', '" + GC_Order.ToString() + "', '" + dicGoodsClass["gc_level"].ToString() + "', '" + dicGoodsClass["gc_flag"].ToString() + "')";
            return CommonBll.ExecuteQuery(sqlStr, pDataBase);
        }

        /// <summary>
        /// 删除商品分类
        /// </summary>
        public static int DeleteGoodsClass(string GC_Id, string pDataBase)
        {
            if (IsExistGoods(GC_Id, pDataBase) > 0)
                return 0;
            else
            {
                string sqlStr = "delete from bi_GoodsClass where GC_Id in(" + GC_Id + ")";
                return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
            }
        }


        /// <summary>
        /// 发布商品分类
        /// </summary>
        public static int ReleaseGoodsClass(string GC_IdList, string pFlag, string gc_type,string pDataBase)
        {
            string sqlStr = "update bi_GoodsClass set GC_Flag='' where  GC_Id in(select GC_Id from bi_GoodsClass_v where (GC_PId='" + gc_type + "' or gcpid= '" + gc_type + "')); UPDATE dbo.bi_GoodsClass SET GC_Flag = '" + pFlag + "' where GC_Id in(" + GC_IdList + ")";
            return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
        }
    }
}
